package com.bocim.course.controller;

import com.bocim.course.meta.Orders;
import com.bocim.course.meta.Product;
import com.bocim.course.meta.User;
import com.bocim.course.service.ProductService;
import com.bocim.course.service.UserService;
import com.bocim.course.serviceImp.IProductService;
import com.bocim.course.serviceImp.IUserService;
import com.sun.javafx.sg.prism.NGShape;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by liujunchen on 2016/11/9.
 */
@Controller
@SessionAttributes("user")
public class ApiFunctionController {
    @Autowired
    private  IProductService  productService ;
    @Autowired
    private  IUserService  userService   ;
    @RequestMapping(value = "/api/login", method = RequestMethod.POST)
    public ModelMap validateLogon(HttpServletRequest request , ModelMap modelMap){
        String  userName =  request.getParameter("userName") ;
        String   password  = request.getParameter("password")  ;
//        IUserService userService = new UserService() ;
        User user= userService.validateLogin(userName , password );
//       System.out.println(userName+":"+password);
        if(user!=null ){
            //登陆成功
            modelMap.addAttribute("code",200);
            modelMap.addAttribute("message", "ok!");
            modelMap.addAttribute("result",true) ;
            modelMap.addAttribute("user", user);
            //把对应验证通过的验证信息加到session中区
            request.getSession().setAttribute("user",user );
        }else
        {
            //登录失败
            modelMap.addAttribute("code",400);
            modelMap.addAttribute("message", "failed");
            modelMap.addAttribute("result",false) ;
        }
        return  modelMap ;
    }
    @RequestMapping(value = "/api/delete" , method = RequestMethod.POST)
    public  ModelMap deleteProducts(@RequestParam("id")  int  productId  ,ModelMap modelMap ){
//        IProductService  productService =   new ProductService() ;
        if(productService.deleteProduct(productId))
        {
            modelMap.addAttribute("code",200);
            modelMap.addAttribute("message", "delete success!");
            modelMap.addAttribute("result",true) ;
        }else
        {
            //登录失败
            modelMap.addAttribute("code",400);
            modelMap.addAttribute("message", "delete failed!");
            modelMap.addAttribute("result",false) ;
        }
        return modelMap ;
    }
    @RequestMapping(value = "/api/buy",method = RequestMethod.POST)
    public ModelMap  buyProducts(@RequestParam("id")  int productId  , ModelMap modelMap , HttpSession session ){
           User user =(User)  session.getAttribute("user")   ;
        Orders orders   =  new Orders()  ;
        orders.setProductId(productId);
        orders.setUserId(user.getUserId());
        Product product = productService.selectById(productId , 0 ) ;
        orders.setAmount(1);
        orders.setBuyPrice(product.getPrice());
        productService.buyProduct(orders );
        modelMap.addAttribute("code",200);
        modelMap.addAttribute("message", "buy success!");
        modelMap.addAttribute("result",true) ;
        return  modelMap ;
    }
}
